<?php

declare(strict_types=1);

namespace App\Wxc\Model;

use App\Wxc\Enums\SharerFormStatus;
use Carbon\Carbon;
use Deepwell\HyperfUid\Snowflake;
use Hyperf\Database\Model\Relations\BelongsTo;
use Hyperf\Database\Model\SoftDeletes;
use Mine\MineModel;

/**
 * @property int $form_id 申请单ID
 * @property int $status 状态。0：待处理；200：审核通过；255：审核不通过
 * @property string $status_desc 状态描述
 * @property string $reason 原因
 * @property int $site_id
 * @property int $shop_id 微信小店id
 * @property string $app_id 微信小店appid
 * @property string $openid 分享员助手小程序用户openid
 * @property string $wechat_account 申请人微信账号
 * @property string $mobile_number 申请人手机号
 * @property string $nickname 昵称
 * @property string $avatar 头像url
 * @property string $bind_qrcode 头像url
 * @property Carbon $created_at 提交时间
 * @property Carbon $updated_at 更新时间
 * @property Carbon $deleted_at
 * @property ?WxcShopInfo $shopInfo
 * @property ?WxcSharerMpUser $mpUser
 */
class WxcSharerForm extends MineModel
{
    use Snowflake;
    use SoftDeletes;

    protected string $primaryKey = 'form_id';
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'wxc_sharer_form';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['form_id', 'status', 'reason', 'site_id', 'shop_id', 'app_id', 'openid', 'wechat_account', 'mobile_number', 'nickname', 'avatar', 'bind_qrcode', 'created_at', 'updated_at', 'deleted_at'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['form_id' => 'string', 'status' => 'integer', 'site_id' => 'string', 'shop_id' => 'string', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'datetime'];

    protected array $appends = [
        'status_desc'
    ];

    public function shopInfo(): BelongsTo
    {
        return $this->belongsTo(WxcShopInfo::class, 'shop_id', 'shop_id');
    }

    public function mpUser(): BelongsTo
    {
        return $this->belongsTo(WxcSharerMpUser::class, 'openid', 'openid');
    }

    public function getStatusDescAttribute($value): string
    {
        if (!isset($this->attributes['status'])) {
            return '';
        }
        return match ($this->attributes['status']) {
            SharerFormStatus::Pending->value => '待处理',
            SharerFormStatus::Approved->value => '审核通过',
            SharerFormStatus::Rejected->value => '审核不通过',
            default => '未知'
        };
    }
}
